﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcContrib.Pagination;

using ENail.Domain.Core;
using Equation.Enterprise.Framework.Domain.Model;
using Equation.Enterprise.Framework.Web.Model;
using Equation.Enterprise.Utilities.DesignByContract;
using ENail.Domain.Core.Contract.Services;


namespace ENail.Portal.Web.Models
{
    public class RegistrationViewModel :
        BaseViewModel<Int16>
    {
        public short CustomerId { get; set; }
        public Status Status { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        public IEnumerable<Customer> CustomerList { get; set; }
    }

    public class RegistrationListViewItem : BaseViewModel<Int16>
    {
        public short CustomerId { get; set; }
        public string CustomerName { get; set; }
        public Status Status { get; set; }
        public DateTime StartTime { get; set; }
        public bool IsSelected { get; set; }
    }

    public class RegistrationViewModelMapper : ViewModelMapper<RegistrationViewModel, Registration>
    {
        private IRegistrationService _registrationService;
        private ICustomerService _customerService;

        public RegistrationViewModelMapper(IRegistrationService registrationService,
                                          ICustomerService customerService)
        {
            _registrationService = registrationService;
            _customerService = customerService;
        }

        public override void MapDomainToView(Registration domainModel, RegistrationViewModel viewModel)
        {
            Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.Id = domainModel.Id;
            viewModel.CustomerId = domainModel.Customer == null ? (short)0 : (short)domainModel.Customer.Id;
            viewModel.Status = domainModel.Status;
            viewModel.StartTime = domainModel.StartTime;
            viewModel.EndTime = domainModel.EndTime;

            viewModel.CustomerList = _customerService.GetAllActiveCustomers();
        }

        public override void MapViewToDomain(RegistrationViewModel viewModel, Registration domainModel)
        {
            Check.Require(viewModel != null);
            Check.Require(domainModel != null);

            domainModel.Customer = viewModel.CustomerId == 0 ? new Customer() : _customerService.GetCustomer(viewModel.CustomerId);
            domainModel.Status = viewModel.Status;
            domainModel.StartTime = viewModel.StartTime;
            domainModel.EndTime = viewModel.EndTime;
        }
    }

    public class RegistrationListViewModelMapper : ListViewModelMapper<RegistrationListViewItem, Registration>
    {
        public const int DEFAULT_PAGE = 1;
        public const int PAGE_SIZE = 999;

        //public override void MapDomainToView(List<Registration> domainModel, GenericListViewModel<RegistrationListViewItem> viewModel)
        //{
        //    Check.Require(domainModel != null);
        //    Check.Require(viewModel != null);

        //    viewModel.List = (from e in domainModel
        //                      where e.IsActive && (viewModel.Filter == null || viewModel.Filter.Trim() == "" || e.Customer.FirstName.ToLower().Contains(viewModel.Filter.Trim().ToLower()))
        //                      select new RegistrationListViewItem
        //                      {
        //                          Id = e.Id,
        //                          CustomerFirstName = e.Customer.FirstName,
        //                          CustomerLastName = e.Customer.LastName,
        //                          CustomerId = e.Customer == null ? (short)0 : (short)e.Customer.Id,
        //                          Status = e.Status,
        //                          StartTime = e.StartTime,
        //                          IsSelected = false
        //                      })
        //                      .OrderByDescending(x => x.StartTime)
        //                      .AsPagination(viewModel.Page ?? DEFAULT_PAGE, PAGE_SIZE);
        //}

        public override void MapDomainToView(List<Registration> domainModel, GenericListViewModel<RegistrationListViewItem> viewModel)
        {
            Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.List = (from e in domainModel
                              where e.Status != Status.Closed &&
                                    (viewModel.Filter == null ||
                                     viewModel.Filter.Trim() == "" ||
                                     (e.Customer.FirstName !=null && e.Customer.FirstName.ToLower().Contains(viewModel.Filter.Trim().ToLower())) ||
                                     (e.Customer.LastName!=null && e.Customer.LastName.ToLower().Contains(viewModel.Filter.Trim().ToLower())))
                              select new RegistrationListViewItem
                                         {
                                             Id = e.Id,
                                             CustomerId = e.Customer == null ? (short) 0 : (short) e.Customer.Id,
                                             CustomerName = e.Customer.FirstName + ", " + e.Customer.LastName,
                                             Status = e.Status,
                                             StartTime = e.StartTime,
                                             IsSelected = false
                                         })
                .OrderBy(x => x.StartTime)
            .AsPagination(viewModel.Page ?? DEFAULT_PAGE, PAGE_SIZE);
        }

        public override void MapViewToDomain(GenericListViewModel<RegistrationListViewItem> viewModel, List<Registration> domainModel)
        {
            throw new NotImplementedException();
        }
    }
}